#!/bin/bash

echo "Starting test for protocol compatibility..."
echo ""

# Function to handle cleanup
cleanup() {
    echo "Cleaning up processes..."
    # Kill any running Java processes related to our servers
    pkill -f "gate-server"
    pkill -f "spring-boot:run"
    exit 0
}

# Set up trap to call cleanup on script exit
trap cleanup EXIT

echo "Step 1: Starting Gate Server..."
cd gate-server
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dserver.port=40001" &
SERVER_PID=$!
cd ..

echo "Waiting for server to start..."
sleep 10

echo "Step 2: Running Protocol Robot Client..."
cd robot-client
java -cp "target/classes:../common/target/classes" com.gzc.just.play.last.war.robot.client.RobotProtocolClient &
CLIENT_PID=$!
cd ..

# Wait for client to finish
wait $CLIENT_PID

echo "Test completed."